package com.example.controller;

import com.example.interceptor.Auth;
import com.example.model.Admin;
import com.example.service.UserService;
import com.example.util.DataMap;
import com.example.util.ErrorStatusEnum;
import com.example.util.MD5Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * User: tao95
 * Date: 2017/5/8
 * Time: 14:46
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Controller
@RequestMapping("user")
@Api(value = "userCenter",tags = "用户中心类")
public class UserController {


    @Autowired
    private UserService userServiceImpl;

    @RequestMapping(value = "/login",produces = MediaType.APPLICATION_JSON_UTF8_VALUE,method = {RequestMethod.POST})
    @ApiOperation("登录接口")
    public ModelAndView login(@ApiParam(name = "queryParam",value = "用户的查询信息",required = true)@RequestParam String  queryParam,
                              @ApiParam(name = "password",value = "用户的密码",required = true)@RequestParam String  password,HttpServletRequest request){
        HttpSession session = request.getSession();
        ModelAndView model = new ModelAndView();
        DataMap map = userServiceImpl.findUser(queryParam, password);//根据用户名或邮箱与密码查找用户，返回操作状态码且成功时找到的用户信息
        if((Integer)map.get("code") != 10000){
            model.setViewName("login");
            model.addObject("loginFail", ErrorStatusEnum.PASSWORD_ERROR.getErrorMsg());
            System.out.println(ErrorStatusEnum.PASSWORD_ERROR.getErrorMsg());
            return model;
        }
        Admin admin = (Admin)map.get("admin");
        session.setAttribute("admin",admin);
        session.setAttribute("sessionFlag",MD5Util.encodeToHex(admin.getPhone()+admin.getPassword()));
        model.setViewName("index");
        return model;
    }
    @Auth
    @RequestMapping(value = "/loginOut",produces = MediaType.APPLICATION_JSON_UTF8_VALUE,method = {RequestMethod.GET})
    @ApiOperation("退出登录接口")
    public ModelAndView loginOut(HttpServletRequest request){
        HttpSession session = request.getSession();
        ModelAndView model = new ModelAndView("index");
        session.setAttribute("admin",null);
        model.setViewName("index");
        return model;
    }
    @Auth
    @RequestMapping(value = "/repeatPass",produces = MediaType.APPLICATION_JSON_UTF8_VALUE,method = {RequestMethod.POST})
    @ApiOperation("重置密码接口")
    public ModelAndView repeatPass(
            @ApiParam(name = "oldPass",value = "用户的查询信息",required = true)@RequestParam String  mpass,
            @ApiParam(name = "newPass",value = "用户的密码",required = true)@RequestParam String  newpass,
            HttpServletRequest request){
        HttpSession session = request.getSession();
        Admin admin = (Admin)session.getAttribute("admin");
        ModelAndView model = new ModelAndView("index");
        DataMap dataMap = userServiceImpl.repeatPass(admin,mpass,newpass);
        if((Integer)dataMap.get("code") == 10001){
            admin.setPassword(MD5Util.encodeToHex(newpass));
            model.addObject("editPass","success");
        }else{model.addObject("editPass","fail");}
        session.setAttribute("admin",admin);
        model.setViewName("pass");
        return model;
    }
    @Auth
    @RequestMapping(value = "/addAdmin",produces = MediaType.APPLICATION_JSON_UTF8_VALUE,method = {RequestMethod.POST})
    @ApiOperation("添加管理员接口")
    public String  addAdmin(
            @ApiParam(name = "Admin",value = "管理员",required = true) Admin admin,
            HttpServletRequest request){
        ModelAndView model = new ModelAndView();
        DataMap dataMap = userServiceImpl.insertAdmin(admin);
        if((Integer)dataMap.get("code") == 10000){
            model.addObject("addPass","success");
        }else{
            model.addObject("addPass","Fail");
        }
        model.setViewName("addAdmin");
        return "forward:/adminList";
    }
    @Auth
    @RequestMapping(value = "/delAdmin",produces = MediaType.APPLICATION_JSON_UTF8_VALUE,method = {RequestMethod.GET})
    @ApiOperation("删除管理员接口")
    public String  delAdmin(
            @ApiParam(name = "id",value = "管理员",required = true) Integer id,
            HttpServletRequest request){
        ModelAndView model = new ModelAndView();
        DataMap dataMap = userServiceImpl.delAdmin(id);
        if((Integer)dataMap.get("code") == 10000){
            model.addObject("addPass","success");
        }else{
            model.addObject("addPass","Fail");
        }
        model.setViewName("addAdmin");
        return "forward:/adminList";
    }

}